//
//  HomeRootViewController.m
//  ioschishan
//
//  Created by 马田成 on 2017/7/27.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeRootViewController.h"
#import "ZJScrollPageView.h"
#import "HomeViewController.h"
#import "HomeNewViewController.h"
#import "HomeWatherViewController.h"
#import "HomeReleaseViewController.h"

@interface HomeRootViewController ()<ZJScrollPageViewDelegate>

@property (weak, nonatomic) ZJContentView *contentView;
@property (weak, nonatomic) ZJScrollSegmentView *segmentView;
@property(strong, nonatomic)NSMutableArray *titles;



@property(strong, nonatomic)NSArray<UIViewController<ZJScrollPageViewChildVcDelegate> *> *childVcs;

@end

@implementation HomeRootViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    
    // Do any additional setup after loading the view.
}
- (void)creationView{

    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1), scrWidth(), screenScale(0.7))];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
    self.titles = @[@"热门",
                    @"最新",
                    @"领水滴",
                    ].mutableCopy;
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    self.automaticallyAdjustsScrollViewInsets = NO;
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.autoAdjustTitlesWidth = YES;
    style.adjustCoverOrLineWidth = YES;
    style.normalTitleColor = [UIColor colorWithRed:51 / 255.0f green:51 / 255.0f blue:51 / 255.0f alpha:1];
    
    
    style.selectedTitleColor = [UIColor colorWithRed:51 / 255.0f green:51 / 255.0f blue:51 / 255.0f alpha:1];
    style.scrollLineColor = [UIColor colorWithRed:216 / 255.0f green:10 / 255.0f blue:9 / 255.0f alpha:1];
    
    style.segmentHeight = 40;
    self.childVcs = [self setupChildVc];
    
    __weak typeof(self) weakSelf = self;
    ZJScrollSegmentView *segment = [[ZJScrollSegmentView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH - 40, 28.0) segmentStyle:style delegate:self titles:self.titles titleDidClick:^(ZJTitleView *titleView, NSInteger indexID) {
        
        [weakSelf.contentView setContentOffSet:CGPointMake(weakSelf.contentView.bounds.size.width * indexID, 0.0) animated:YES];
        
    }];
    
    self.segmentView = segment;
    self.navigationItem.titleView = self.segmentView;
    
    ZJContentView *content = [[ZJContentView alloc] initWithFrame:CGRectMake(0, 1, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49) segmentView:self.segmentView parentViewController:self delegate:self];
    
    self.contentView = content;
    [self.view addSubview:self.contentView];
    UIButton *buttonAdd = [[UIButton alloc] init];
    
    buttonAdd.frame = CGRectMake(DEF_SCREEN_WIDTH - 50, 0, 40, 40);
    
    [buttonAdd setImage:[UIImage imageNamed:@"launch_icon"] forState:UIControlStateNormal];
    [buttonAdd addTarget:self action:@selector(ReleaseClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:buttonAdd];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    
    
    NSLog(@"aaaaaaaa%@", self.titles);
}
#pragma mark ----------------ZJScrollPageViewDelegate---------------------
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (NSArray *)setupChildVc {
    
    HomeViewController *vc1 = [HomeViewController new];
    
    HomeNewViewController *vc2 = [HomeNewViewController new];
    
    
    HomeWatherViewController *vc3 = [HomeWatherViewController new];
    
    NSArray *childVcs = [NSArray arrayWithObjects:vc1, vc2, vc3, nil];
    return childVcs;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = self.childVcs[index];
    }
    
    return childVc;
}

- (void)ReleaseClick{

    HomeReleaseViewController *vc = [[HomeReleaseViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
